<script setup>

import {useLoginStore} from "@/sotre/useLoginStore.js";
import {onMounted, ref} from "vue";
import {reqInstance} from "@/utils/request/index.js";
import dayjs from "dayjs";
import AddEdit from "@/views/receivable/AddEdit.vue";
import {message} from "ant-design-vue";
import {useCompanyStore} from "@/sotre/useCompanyStore.js";
import Actual from "@/views/receivable/actual.vue";

onMounted(() => {
  getMyContracts()
  getData()
})
const dataSource = ref([])
const current = ref(1)
const pageSize = ref(10)
const total = ref(0)

let myContracts = {};
const getMyContracts = async () => {
  try {
    const resList = await reqInstance({
      url: "/receivable/can-relate-contract",
      method: "get",
      params: {
        com_id: companyStore.nowComID,
      },
    });
    (resList?.data ?? []).forEach(item => {
      myContracts[item.id] = item
    });
  } catch (e) {

  }
}
const companyStore = useCompanyStore()
const status2txt = [
    '未收',
    '部分',
    '已收',
]
const getData = async () => {
  try {
    const res = await reqInstance({
      url: "/receivable",
      method: "get",
      params: {
        per_page: pageSize.value,
        page: current.value,
        com_id: companyStore.nowComID,
      },
    })
    console.log('res', res);
    dataSource.value = (res?.data?.data?.data ?? []).map((item) => {
      console.log('item', item);
      item.project_step_txt = item.project_step === 1 ? '最终收款' : '进行中';
      item.estimated_date_txt = dayjs(item.estimated_date).format('YYYY-MM-DD')
      item.un_actual_money = item.estimated_money - item.actual_money
      item.receivable_status_txt = status2txt[item.receivable_status]
      return item
    });
  } catch (e) {

  }
}

const delOne = async (id) => {
  try {
    await reqInstance({
      url: "/company/" + id,
      method: "delete",
    })
    message.success("删除成功")
    await getData()
  } catch (e) {

  }
}

const columns = [
  {
    title: "归档编号",
    dataIndex: ['contract','contract_no'],
  },
  {
    title: "项目名称",
    dataIndex: ['contract','project_name'],
  },
  {
    title: "项目类型",
    dataIndex: ['project_type_setting','type_title'],
  },
  {
    title: "项目状态",
    dataIndex: "project_step_txt",
  },
  {
    title: "预计收款时间",
    dataIndex: "estimated_date_txt",
  },
  {
    title: "应收金额",
    dataIndex: "estimated_money",
  },
  {
    title: "已收金额",
    dataIndex: "actual_money",
  },
  {
    title: "未收金额",
    dataIndex: "un_actual_money",
  },
  {
    title: "状态",
    dataIndex: "receivable_status_txt",
  },
  {
    dataIndex: 'action',
    title: '操作',
    key: 'action',
  },
]
const sonDrawRef = ref()
const actualDrawRef = ref()
</script>

<template>
  <a-button type="primary" @click="sonDrawRef.openDraw(myContracts,true)">新增应收账款</a-button>
  <AddEdit ref="sonDrawRef" @create-completed="getData()"/>
  <actual ref="actualDrawRef" @create-completed="getData()"/>
  <a-table :dataSource="dataSource" :columns="columns">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'action'">
        <a-button v-if="myContracts?.[record.contract.id]" class="edit-btn" @click="sonDrawRef.updateOpen(myContracts,record)">编辑</a-button>
        <a-button v-if="useLoginStore().isAdmin" class="edit-btn" @click="actualDrawRef.openDraw(record)">实收</a-button>
        <a-button v-if="myContracts?.[record.contract.id] || useLoginStore().isAdmin" @click="delOne(record.id)">删除</a-button>
      </template>
    </template>
  </a-table>
</template>

<style scoped>

</style>